
package info.johnwheeler.gaestorage.core;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.googlecode.objectify.util.DAOBase;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;

public class AudienceDao extends DAOBase {

    static {
        ObjectifyService.register(Audience.class);
    }

    public Audience save(Audience audience) {
        ofy().put(audience);
        return audience;
    }
    
    public Audience findById(Long id) {
        Key<Audience> key = new Key<Audience>(Audience.class, id);
        return ofy().get(key);
    }

    public ArrayList<Audience> findAll() {
    	Objectify ofy = ObjectifyService.begin();
    	Query<Audience> query = ofy.query(Audience.class);
    	QueryResultIterator<Audience> iterator = query.iterator();
    	ArrayList<Audience> audiences = new ArrayList<Audience>();
        while (iterator.hasNext()) {
            Audience audience = iterator.next();
            Person author = ofy.get(Person.class, audience.getAuthorKey().getId());
            Person owner = ofy.get(Person.class, audience.getAuthorKey().getId());
            audience.setAuthor(author);
            audience.setOwner(owner);
            audiences.add(audience);
        }
        return audiences; 
    }
    
    public Audience populatePersons(Audience audience){
    	Objectify ofy = ObjectifyService.begin();
    	Person author = ofy.get(Person.class, audience.getAuthorKey().getId());
        Person owner = ofy.get(Person.class, audience.getAuthorKey().getId());
        audience.setAuthor(author);
        audience.setOwner(owner);
        return audience;
    }

}
